//
//  EmaOverseaSdkApi_Android.cpp
//  clientOversea
//
//  Created by ming on 2019/1/9.
//

#include "EmaOverseaSdkApi.hpp"
#include "cocos2d.h"
#include <jni.h>
#include "platform/android/jni/JniHelper.h"

#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"

using namespace cocos2d;

#define EmaOverseaSdkJavaClass "com/repong/framework/ChannelAPI"

//std::string EmaOverseaSdkApi::getVersionNum(){
//    JniMethodInfo t;
//    std::string ret = "";
//    if(JniHelper::getStaticMethodInfo(t, EmaOverseaSdkJavaClass, "getVersionNum", "()Ljava/lang/String;"))
//    {
//        jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
//        ret = JniHelper::jstring2string(str);
//        t.env->DeleteLocalRef(str);
//        t.env->DeleteLocalRef(t.classID);
//    }
//    return ret;
//}

std::string EmaOverseaSdkApi::getVersion(){
    JniMethodInfo t;
    std::string ret = "";
    if(JniHelper::getStaticMethodInfo(t, EmaOverseaSdkJavaClass, "getVersion", "()Ljava/lang/String;"))
    {
        jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
        ret = JniHelper::jstring2string(str);
        t.env->DeleteLocalRef(str);
        t.env->DeleteLocalRef(t.classID);
    }
    return ret;
}

std::string EmaOverseaSdkApi::getResourceVersion()
{
    JniMethodInfo t;
    std::string ret = "";
    if(JniHelper::getStaticMethodInfo(t, EmaOverseaSdkJavaClass, "getResourceVersion", "()Ljava/lang/String;"))
    {
        jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
        ret = JniHelper::jstring2string(str);
        t.env->DeleteLocalRef(str);
        t.env->DeleteLocalRef(t.classID);
    }
    return ret;
}

std::string EmaOverseaSdkApi::getCdn(){
    JniMethodInfo t;
    std::string ret = "";
    if(JniHelper::getStaticMethodInfo(t, EmaOverseaSdkJavaClass, "getCdn", "()Ljava/lang/String;"))
    {
        jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
        ret = JniHelper::jstring2string(str);
        t.env->DeleteLocalRef(str);
        t.env->DeleteLocalRef(t.classID);
    }
    return ret;
}

bool EmaOverseaSdkApi::isInitialized(){
    JniMethodInfo t;
    int ret = -1;
    if(JniHelper::getStaticMethodInfo(t, EmaOverseaSdkJavaClass, "getInitState", "()I"))
    {
        ret = t.env->CallStaticIntMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
    return ret == 0;
}

void EmaOverseaSdkApi::jumpToAppShop(){
    JniMethodInfo t;
    if(JniHelper::getStaticMethodInfo(t, EmaOverseaSdkJavaClass, "jumpToAppShop", "()V"))
    {
        t.env->CallStaticVoidMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
}

bool EmaOverseaSdkApi::isReview(){
    return false;
}

int EmaOverseaSdkApi::getInitSdkState(){
    JniMethodInfo t;
    int ret = -1;
    if(JniHelper::getStaticMethodInfo(t, EmaOverseaSdkJavaClass, "getInitState", "()I"))
    {
        ret = t.env->CallStaticIntMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
    return ret;
}

void EmaOverseaSdkApi::initSdk(){
    JniMethodInfo t;
    if(JniHelper::getStaticMethodInfo(t, EmaOverseaSdkJavaClass, "initSDK", "()V"))
    {
        t.env->CallStaticVoidMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
}


bool EmaOverseaSdkApi::checkPermissions(){
    JniMethodInfo t;
    bool ret = false;
    if(JniHelper::getStaticMethodInfo(t, EmaOverseaSdkJavaClass, "checkPermissions", "()Z"))
    {
        ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
    return ret;
}

void EmaOverseaSdkApi::requestPermissions(){
    JniMethodInfo t;
    if(JniHelper::getStaticMethodInfo(t, EmaOverseaSdkJavaClass, "requestPermissions", "()V"))
    {
        t.env->CallStaticVoidMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
}

void EmaOverseaSdkApi::goAppSetting(){
    JniMethodInfo t;
    if(JniHelper::getStaticMethodInfo(t, EmaOverseaSdkJavaClass, "goAppSetting", "()V"))
    {
        t.env->CallStaticVoidMethod(t.classID, t.methodID);
        t.env->DeleteLocalRef(t.classID);
    }
}


void EmaOverseaSdkApi::activeBuriedPointReport(const char* data){
    JniMethodInfo t;
    if (JniHelper::getStaticMethodInfo(t, EmaOverseaSdkJavaClass, "activeBuriedPointReport", "(Ljava/lang/String;)V")) {
        jstring jsonStr = t.env->NewStringUTF(data);
        t.env->CallStaticVoidMethod(t.classID, t.methodID, jsonStr);
        t.env->DeleteLocalRef(jsonStr);
        t.env->DeleteLocalRef(t.classID);
    }
}

// --- eventName 事件名称 active AdJust识别码
void EmaOverseaSdkApi::reportActiveBuriedPoint(const char * trigger, int result, int duration){
    rapidjson::StringBuffer s;
    rapidjson::Writer<rapidjson::StringBuffer> writer(s);
    writer.StartObject();
    writer.Key("event");
    writer.String("dc_activation_log");
    writer.Key("channels");
    writer.StartArray();
    writer.String("appsflyer");
    writer.String("adjust");
    writer.EndArray();
    writer.Key("params");
    writer.StartObject();
    writer.Key("dc_activation_log");
    writer.String("gsnapx");
    writer.Key("result");
    writer.Int(result);
    writer.Key("trigger");
    writer.String(trigger);
    if(duration != 0){
        writer.Key("duration");
        writer.Int(duration);
    }
    writer.EndObject();
    writer.EndObject();
    std::string jsonStr = s.GetString();
    activeBuriedPointReport(jsonStr.c_str());
}


